package com.dawn.module.hospital.controller.admin.invoice;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.util.object.BeanUtils;
import static com.dawn.framework.common.pojo.CommonResult.success;

import com.dawn.framework.excel.core.util.ExcelUtils;

import com.dawn.framework.apilog.core.annotation.ApiAccessLog;
import static com.dawn.framework.apilog.core.enums.OperateTypeEnum.*;

import com.dawn.module.hospital.controller.admin.invoice.vo.*;
import com.dawn.module.hospital.dal.dataobject.invoice.InvoiceDO;
import com.dawn.module.hospital.service.invoice.InvoiceService;

@Tag(name = "管理后台 - 发票")
@RestController
@RequestMapping("/hospital/invoice")
@Validated
public class InvoiceController {

    @Resource
    private InvoiceService invoiceService;

    @PostMapping("/create")
    @Operation(summary = "创建发票")
    @PreAuthorize("@ss.hasPermission('hospital:invoice:create')")
    public CommonResult<Long> createInvoice(@Valid @RequestBody InvoiceSaveReqVO createReqVO) {
        return success(invoiceService.createInvoice(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新发票")
    @PreAuthorize("@ss.hasPermission('hospital:invoice:update')")
    public CommonResult<Boolean> updateInvoice(@Valid @RequestBody InvoiceSaveReqVO updateReqVO) {
        invoiceService.updateInvoice(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除发票")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hospital:invoice:delete')")
    public CommonResult<Boolean> deleteInvoice(@RequestParam("id") Long id) {
        invoiceService.deleteInvoice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得发票")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hospital:invoice:query')")
    public CommonResult<InvoiceRespVO> getInvoice(@RequestParam("id") Long id) {
        InvoiceDO invoice = invoiceService.getInvoice(id);
        return success(BeanUtils.toBean(invoice, InvoiceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得发票分页")
    @PreAuthorize("@ss.hasPermission('hospital:invoice:query')")
    public CommonResult<PageResult<InvoiceRespVO>> getInvoicePage(@Valid InvoicePageReqVO pageReqVO) {
        PageResult<InvoiceDO> pageResult = invoiceService.getInvoicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InvoiceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出发票 Excel")
    @PreAuthorize("@ss.hasPermission('hospital:invoice:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInvoiceExcel(@Valid InvoicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InvoiceDO> list = invoiceService.getInvoicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "发票.xls", "数据", InvoiceRespVO.class,
                        BeanUtils.toBean(list, InvoiceRespVO.class));
    }

}